package com.fxu.tpl.api;

import com.fxu.framework.core.base.Result;
import com.fxu.framework.core.service.impl.ApiServiceImpl;
import com.fxu.framework.core.sql.SQuery;
import com.fxu.framework.core.sql.SUtils;
import com.fxu.tpl.dto.TestPageReq;
import com.fxu.tpl.entity.Test;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/***
 * <p>fxu</p>
 * 测试的Restfull接口
 * @author fangxu
 * @version 2023-01-05
 ***/
@RestController
@RequestMapping("/api/test")
@Api(tags = "测试接口")
@RequiredArgsConstructor
public class TestController {
    private final ApiServiceImpl apiService;

    @PostMapping("/saveBatch/")
    @ApiOperation("批量保存")
    public Result<Boolean> saveBatch(@Valid @RequestBody List<Test> list, HttpServletRequest request) {
        // TokenDto tokenDto = TokenHolder.getTokenHolder().getTokenDtoNotNull(request, TokenDto.class);
        return Result.ok(apiService.saveBatch(list));
    }

    @PostMapping("/pageBy/")
    @ApiOperation("分页列表")
    public Result<List<Test>> pageBy(@Valid @RequestBody TestPageReq req, HttpServletRequest request) {
        // TokenDto tokenDto = TokenHolder.getTokenHolder().getTokenDtoNotNull(request, TokenDto.class);
        SQuery<Test> sQuery = SUtils.query(Test.class);
/*=== 自动生成代码 开始 ===*/
        sQuery.eq(Test::getId, req.getId());
        sQuery.likeRight(Test::getName, req.getName());
/*=== 自动生成代码 结束 ===*/
        return Result.ok(apiService.pageBy(req.getPageNum(), req.getPageSize(), sQuery));
    }

}